package ca.intelliware.commons.virtualization.vmware;

import java.io.IOException;
import java.util.Collection;

import ca.intelliware.commons.virtualization.VMachine;
import ca.intelliware.commons.virtualization.VirtualMachineGroup;

public class VmGroup implements VirtualMachineGroup {
	private final String id;
	private final Collection<VMachine> virtualMachines;

	VmGroup(String groupId, Collection<VMachine> virtualMachines) {
		this.id = groupId;
		this.virtualMachines = virtualMachines;
	}

	public String getId() {
		return this.id;
	}

	public String inventoryReport() throws Exception {
		StringBuilder strBuilder = new StringBuilder();
		strBuilder.append(this.getId()).append("\n");
		strBuilder.append("   ");
		for (VMachine vmMachine : virtualMachines()) {
			strBuilder.append(vmMachine.getId()).append(",");
		}
		strBuilder.append("\n");
		return strBuilder.toString();
	}

	public void revertAllVirtualMachinesToLastSnapshot() throws Exception {
		for (VMachine virtualMachine : virtualMachines()) {
			virtualMachine.revertToLastSnapshot();
		}

	}

	public VMachine virtualMachine(String id) throws IOException {
		VMachine found = null;
		for (VMachine virtualMachine : virtualMachines()) {
			if (virtualMachine.getId().equalsIgnoreCase(id)) {
				found = virtualMachine;
				break;
			}
		}
		return found;
	}

	public Collection<VMachine> virtualMachines() throws IOException {
		return this.virtualMachines;
	}

}
